﻿using System;
using System.Text;
using ChatCommon.Packets;
using NetBrick.Server;

namespace ChatServer.Handler
{
    public class AuthenticatePacketHandler : PacketHandler
    {
        public AuthenticatePacketHandler(Server server) : base(server)
        {
        }

        public override void Process(Object packetData, BasePeer peer)
        {
            var authPacket = (AuthenticatePacket) packetData;
            var chatPeer = (ChatPeer) peer;
            var encryptedName = authPacket.Name;
            Log.Info("Encrypted Name: {0}", encryptedName);
            var decryptedName = Encoding.UTF8.GetString(peer.Decrypt(Convert.FromBase64String(encryptedName)));
            Log.Info("Decrypted Name: {0}", decryptedName);
            chatPeer.Authenticate(decryptedName);
        }
    }
}